import { useUserStore } from "@/store/modules/user";
// User permissions related operations
export enum RoleEnum {
  IS_PROJECT = "isProject", // 项目部
  IS_SUPPLIER = "isSupplier" // 供应商
}

export function usePermission() {
  const userStore = useUserStore();
  /**
   * Determine whether there is permission
   * hasPermission(['isProject'])
   * hasPermission('isProject')
   */
  function hasPermission(
    value?: RoleEnum | RoleEnum[] | string | string[],
    def = true
  ): boolean {
    // Return default if no value is provided
    if (!value) {
      return def;
    }

    const roles = userStore.roleList; // 从 getter 获取角色状态

    // 如果传入数组，检查数组中是否有任何一个角色符合条件
    if (Array.isArray(value)) {
      return value.some(role => roles[role as keyof typeof roles]);
    }

    // 如果传入单个字符串，直接检查该角色是否存在
    return !!roles[value as keyof typeof roles];
  }

  return { hasPermission };
}
